package com.loongtech.bi.manager.pay;

import com.loongtech.bi.entity.pay.EntityTotalCash;
import com.loongtech.bi.entity.pay.EntityTotalCashTag;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TotalCashManager extends ManagerQueryCacheBase<EntityTotalCash> {
	private static final long serialVersionUID = 1L;
	
	/**
	 * 根据开始结束时间，求付费金额
	 * @param dateStart
	 * @param dateEnd
	 * @param type
	 * @return
	 */
	public List<EntityTotalCash> getTotalCashByDate(Date dateStart, Date dateEnd) {
		return findByQuery("from EntityTotalCash  where 1=1 and day>=:dayStart and day<=:dayEnd order by day", "dayStart", dateStart, "dayEnd", dateEnd);
	}

    /**
     * 根据开始结束时间，求付费金额
     * @param dateStart
     * @param dateEnd
     * @param type
     * @return
     */
    public List<EntityTotalCash> getTotalCashByDate2(Date dateStart, Date dateEnd) {
        return findByQuery("from EntityTotalCash  where 1=1 and day>=:dayStart and day<=:dayEnd and serverid='0' order by day", "dayStart", dateStart, "dayEnd", dateEnd);
    }

    /**
     * 根据开始结束时间服务器id，求付费金额
     * @param dateStart
     * @param dateEnd
     * @return
     */
    public List<EntityTotalCashTag> getTotalCashByDate(String serverid, Date dateStart, Date dateEnd) {
        return findByQuery("from EntityTotalCashTag  where serverid=? and day>=? and day<=? order by day", serverid, dateStart, dateEnd);
    }

    /**
     * 开服至今付费金额
     * @param dateStart
     * @param dateEnd
     * @return
     */
    public Long getTotalCashFromOpenDate(Date dateStart, Date dateEnd) {
        Long ss = getByQuery("select sum(total_cash) from EntityTotalCash where 1=1 and day>=:dayStart and day<=:dayEnd and platformid = '0' and channelid = '0' and serverid = '0'", "dayStart", dateStart, "dayEnd", dateEnd);
        return ss == null ? 0 : ss;
    }

    public List<Object[]> getTotalCashFromOpenDate2(Date dateStart, Date dateEnd) {
        return findByQuery("select platformid, sum(total_cash) from EntityTotalCash where 1=1 and day>=:dayStart and day<=:dayEnd and channelid = '0' and serverid = '0' group by platformid ", "dayStart", dateStart, "dayEnd", dateEnd);
    }

    /**
     * 开服至今付费金额（分平台）
     * @param dateStart
     * @param dateEnd
     * @return
     */
    public Long getTotalCashFromOpenDateByPlatform(Date dateStart, Date dateEnd, String platformid) {
        Long ss = getByQuery("select sum(total_cash) from EntityTotalCash where 1=1 and day>=:dayStart and day<=:dayEnd and platformid =:platformid ", "dayStart", dateStart, "dayEnd", dateEnd, "platformid",platformid);
        return ss == null ? 0 : ss;
    }

	/**
	 * 根据开始结束时间，求付费金额对象
	 * @param dateStart
	 * @param dateEnd
	 * @param type
	 * @return
	 */
	public List<EntityTotalCash> getAllTotalCashByAllKind(Date dateStart, Date dateEnd, String queryPlatform, String queryChannel, String queryServer) {
		return findByQuery("from EntityTotalCash where 1=1 and day>=:dayStart and day<=:dayEnd and platformid = '"+queryPlatform+"' and channelid = '"+queryChannel+"' and serverid = '"+queryServer+" ' and total_cash is not null", "dayStart", dateStart, "dayEnd", dateEnd);
	}
	
	/**
	 * 根据开始结束时间，求付费金额
	 * @param dateStart
	 * @param dateEnd
	 * @param type
	 * @return
	 */
	public List<Object[]> getTotalCashByAllKind(Date dateStart, Date dateEnd, String queryPlatform, String queryChannel, String queryServer) {
		return findByQuery("select date_format(day,'%Y-%m-%d'),total_cash from EntityTotalCash where 1=1 and day>=:dayStart and day<=:dayEnd and platformid = '"+queryPlatform+"' and channelid = '"+queryChannel+"' and serverid = '"+queryServer+" ' and total_cash is not null", "dayStart", dateStart, "dayEnd", dateEnd);
	}
	
	/**
	 * 根据开始结束时间，求付费金额
	 * @param dateStart
	 * @param dateEnd
	 * @param type
	 * @return
	 */
	public List<Object[]> getSumTotalCashByAllKind(Date dateStart, Date dateEnd, String queryPlatform, String queryChannel, String queryServer) {
		return findByQuery("select sum(total_cash)  from EntityTotalCash where 1=1 and day>=:dayStart and day<=:dayEnd and platformid = '"+queryPlatform+"' and channelid = '"+queryChannel+"' and serverid = '"+queryServer+" ' and total_cash is not null", "dayStart", dateStart, "dayEnd", dateEnd);
	}
}
